const express=require("express");

const query=require("../pool");

const vertify=require("../modules/token")

const moment=require("moment")

const order=express.Router();

// 订单页面获取商品详情
order.get('/settle',async(req,res)=>{
  let obj=req.query;
  // console.log(obj);
  let uid=vertify.verifytoken(obj[0]);
  if(!uid.uid){
  //console.log("用户名不能为空")
  }else{
    let result=await query(`select cake_card.count,cakelist.cid,cakelist.cake_price,cakelist.cake_description,cakelist.cakename,cakelist.cake_pic from cake_card,cakelist where cake_card.product_id=cakelist.cid and cake_card.checked=1 and cake_card.user_id=${uid.uid};`);
    // console.log(result)
    if(result.length!=0){
      res.send({code:1,data:result});
    }
  }
})

// 订单页面获取立即购买的商品
order.get('/buy',async(req,res)=>{
  let obj=req.query;
  // console.log(obj);
  let result=await query(`select cid,cake_price,cake_description,cakename,cake_pic from cakelist where cid=${obj.cid};`);
  console.log(result)
  if(result.length!=0){
    res.send({code:1,data:result});
  }
})


// 将商品推送到订单表和订单详情表中
order.get('/set',async(req,res)=>{
  let obj=req.query;
  // console.log(obj);
  let uid=vertify.verifytoken(obj.token);
  let total=obj.total_price/100;
  if(!uid.uid){
  //console.log("用户名不能为空")
  }else{
    let order_time=moment().format("YYYY-MM-DD HH:mm:ss");
    // let order_time = new Date().toLocaleTimeString();
    // console.log(order_time)
    let result=await query(`insert into cake_order values (null,${uid.uid},${obj.aid},'待付款','${obj.serve_time}','${order_time}',null,null,null,${obj.orderId},${total});`);
    // console.log(result);
    if(result.affectedRows!=0){
      let result2= await query(`select oid from cake_order where order_number=${obj.orderId};`);
      if(result2.length){
        res.send({code:1,data:result2})
      }
    }
  }
})
order.get('/setDetail',async(req,res)=>{
  let obj=req.query;
  let result=await query(`insert into order_detail values(null,${obj.order_id*1},${obj.product_id*1},${obj.count*1});`);
  // console.log(result)
})

// 通过用户id获取订单表的数据
order.get('/get',async(req,res)=>{
  let obj=req.query;
  let uid=vertify.verifytoken(obj[0]);
  if(!uid.uid){
  //console.log("用户名不能为空")
  }else{
    let result1=await query(`select oid,status,reserve_time,order_number,total_price from cake_order where user_id=${uid.uid} order by oid desc`);
    if(result1.length){
      res.send({code:1,data:result1});
    }
  }
})

// 通过订单表的oid关联查询每个订单的详情
order.get('/getdetail',async(req,res)=>{
  let obj=req.query;
  let result=await query(`select cakelist.cid,cakelist.cakename,cakelist.cake_price,cakelist.cake_pic,order_detail.count from order_detail,cakelist where order_detail.product_id=cakelist.cid and order_detail.order_id=${obj.order_id};`);
  // console.log(result)
  if(result.length){
    res.send({code:1,data:result})
  }
})

// 支付成功之后修改订单状态为待发货并上传付款时间
order.get('/paid',async(req,res)=>{
  let obj=req.query;
  let pay_time=moment().format("YYYY-MM-DD HH:mm:ss");
//console.log(obj);
  let result=await query(`update cake_order set status='待发货',pay_time='${pay_time}' where order_number=${obj.order_number*1};`)
//console.log(result);
  if(result.affectedRows!=0){
    res.send({code: 1,msg: "修改成功"})
  }
})

// 点击确认收货
order.get('/confirm',async(req,res)=>{
  let obj=req.query;
  let result=await query(`update cake_order set status='已收货' where oid=${obj.oid};`);
  if(result.affectedRows!=0){
    res.send({code:1,msg: "修改完成"})
  }
})

// 倒计时结束时改变订单状态
order.get('/finish',async(req,res)=>{
  let obj=req.query;
//console.log(obj)
  let result=await query(`update cake_order set status='已取消' where oid=${obj.oid};`);
  if(result.affectedRows!=0){
    res.send({code:1,msg: "修改完成"})
  }
})

order.get('/getOrder',async(req,res)=>{
  let obj = req.query;
  // console.log(obj);
  // 验证token
  let token_res = vertify.verifytoken(obj.token);
  if(JSON.stringify(token_res)=='{}'){
    res.send({code:0,msg:'token失效,请重新登录'})
  }else{
    let uid = token_res.uid;
    let sql_parts = 'and status = '+`"${obj.orderType}"`
    if(obj.orderType=="全部")sql_parts ='';
    // console.log(sql_parts);
    let sql = `select cu.uname ,co.oid ,c.cakename ,c.cake_pic ,c.cake_description ,co.total_price ,co.reserve_time ,co.status ,od.count  from order_detail od
    left join cake_order co on co.oid = od.order_id
    left join cakelist c on c.cid = od.product_id
    left join cake_user cu on co.user_id = cu.uid
    where 1=1 and cu.uid = ? ${sql_parts}  and c.family = '蛋糕' order by oid desc`;
    let data = await query(sql,[uid])
    res.send({code:1,data})
  }
})

module.exports=order;